package com.hugesoft.jxgs.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.hugesoft.jxgs.R;
import com.hugesoft.jxgs.activity.base.BaseActivity;
import com.hugesoft.logic.managers.LogicManager;
import com.hugesoft.logic.module.DriverLicence;
import com.hugesoft.logic.module.Police;

public class ListActivityPoliceInfo extends BaseActivity {
	ListView mListView;
	private static List<Police> mPoliceList = new ArrayList<Police>();
	public static int RSTSEARCHPOLICE = 1001;
	public static String TAGPOLICENO = "TAGPOLICENO";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activitylist);
		((TextView) findViewById(R.id.titleTextView)).setText("警察列表");
		findViewById(R.id.backImg).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		mPoliceList = LogicManager.policeManager().loadallpolice();

		mListView = (ListView) findViewById(R.id.lvrst);

		mListView.setAdapter(new LvAdapter(ListActivityPoliceInfo.this));
		
		mListView.setOnItemClickListener(new OnItemClickListener() 
		{
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Police police = mPoliceList.get(arg2);
				Intent intent = new Intent();
				intent.putExtra(ListActivityPoliceInfo.TAGPOLICENO, police.getPoliceno());
				setResult(ListActivityPoliceInfo.RSTSEARCHPOLICE,intent);
				finish();
			}
		}
		);
	}

	static class LvAdapter extends BaseAdapter implements SectionIndexer {
		private Context mContext;

		@SuppressWarnings("unchecked")
		public LvAdapter(Context mContext) {
			this.mContext = mContext;
		}

		@Override
		public int getCount() {
			return mPoliceList.size();
		}

		@Override
		public Object getItem(int position) {
			return mPoliceList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Police policeinfo = mPoliceList.get(position);
			ViewHolder viewHolder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.activitylistitempoliceinfo, null);
				viewHolder = new ViewHolder();
				viewHolder.tvPoliceName = (TextView) convertView
						.findViewById(R.id.textPoliceName);
				viewHolder.tvPoliceNo = (TextView) convertView
						.findViewById(R.id.textPoliceNo);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}

			viewHolder.tvPoliceName.setText(policeinfo.getName());
			viewHolder.tvPoliceNo.setText(policeinfo.getPoliceno());
			return convertView;
		}

		static class ViewHolder {
			TextView tvPoliceName;// 姓名
			TextView tvPoliceNo;// 警号
		}

		@Override
		public int getSectionForPosition(int position) {
			return 0;
		}

		@Override
		public Object[] getSections() {
			return null;
		}

		@Override
		public int getPositionForSection(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}
	}
}
